import requests

def get_weather(city):
    # 使用天气预报API的URL
    url = f"http://api.weatherapi.com/v1/forecast.json?key=be0b17a14b4342d8827124718230706&q={city}&days=15"

    # 发送GET请求获取天气数据
    response = requests.get(url)
    data = response.json()

    # 获取当天天气信息
    current = data['current']
    current_date = current['last_updated'].split(' ')[0]
    current_weather = current['condition']['text']
    current_temp_c = current['temp_c']
    current_temp_f = current['temp_f']

    # 获取未来7天天气信息
    forecast_7days = data['forecast']['forecastday'][:7]

    # 获取未来15天天气信息
    forecast_15days = data['forecast']['forecastday']

    # 打印当天天气信息
    print("当前城市：", city)
    print("\n===== 当天天气预报 =====")
    print("日期：", current_date)
    print("天气：", current_weather)
    print("最高温度：", f"{current_temp_c}°C")  # 使用格式化字符串
    print("最低温度：", f"{current_temp_f}°F")  # 使用格式化字符串

    # 打印未来7天天气信息
    print("\n===== 未来7天天气预报 =====")
    for day in forecast_7days:
        date = day['date']
        weather = day['day']['condition']['text']
        max_temp_c = day['day']['maxtemp_c']
        max_temp_f = day['day']['maxtemp_f']
        min_temp_c = day['day']['mintemp_c']
        min_temp_f = day['day']['mintemp_f']

        print("日期：", date)
        print("天气：", weather)
        print("最高温度：", f"{max_temp_c}°C")  # 使用格式化字符串
        print("最低温度：", f"{min_temp_c}°C")  # 使用格式化字符串

    # 打印未来15天天气信息
    print("\n===== 未来15天天气预报 =====")
    for day in forecast_15days:
        date = day['date']
        weather = day['day']['condition']['text']
        max_temp_c = day['day']['maxtemp_c']
        max_temp_f = day['day']['maxtemp_f']
        min_temp_c = day['day']['mintemp_c']
        min_temp_f = day['day']['mintemp_f']

        print("日期：", date)
        print("天气：", weather)
        print("最高温度：", f"{max_temp_c}°C")  # 使用格式化字符串
        print("最低温度：", f"{min_temp_c}°C")  # 使用格式化字符串



# 主程序
city = input("请输入城市名称：")
get_weather(city)
